package comm.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.oreilly.servlet.Base64Encoder;

public class SMTPMailSendManager{
	
	private String protocol = "smtp";
	
	private String type = "text/html; charset=UTF-8";
	
	private String userName = "quiksopt";
	private String password = "quik1234";
	private String host = "smtp.google.com";
	private int port = 25;
	private boolean starttlsEnable = true;

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public void setStarttlsEnable(boolean starttlsEnable) {
		this.starttlsEnable = starttlsEnable;
	}
	
	public SMTPMailSendManager(){
		
	}
	
	public String send(String toAddr,String toName, String fromAddr,
			String fromName, String subject, String content){
		String result = "FAIL";
		try{
		Properties props = new Properties();
		props.put("mail.transport.protocol", protocol);
		props.put("mail.smtp.host", host);
		props.put("mail.smtp.port", String.valueOf(port));
		
		Authenticator authenticator = null;
		if (userName.length()>0) {
			props.put("mail.smtp.auth", "true");
			authenticator = new SMTPAuthenticator(userName, password);
		}
		
		if (starttlsEnable) {
			props.put("mail.smtp.starttls.enable", Boolean.toString(starttlsEnable));	
		}
		
		Session session = Session.getInstance(props, authenticator);

		MimeMessage message = new MimeMessage(session);
		message.setFrom(new InternetAddress(fromAddr, fromName));
		message.addRecipient(Message.RecipientType.TO, new InternetAddress(toAddr, toName));
		message.setSubject(subject);
		//TODO: content에 genContentForm()으로 만든형식과 입력 받은 메세지 content를 삽입하는 식으로 ㄱㄱ
		message.setContent(content, type);

		Transport.send(message);
		System.out.println("메일 전송 성공!!!!+ㅁ+ 왉!!!!");
		result = "SUCCESS";
		} catch (UnsupportedEncodingException e) {
			return result;
		} catch (MessagingException e) {
			return result;
		}
		return result;
	}
	private String genAuthCode(){
		String code = "";
		Date d = new Date();
		code = Base64Encoder.encode(String.valueOf(d.getTime()));
		return code;
	}
	
	private String genContentForm(String content){
		String form = "";
		//TODO: 메일 양식 만들고 인증코드 링크 삽입.(FILE에서 읽어오는 방식이 좋겠다.)
		return form;
	}
	
	class SMTPAuthenticator extends Authenticator {
		
		PasswordAuthentication passwordAuthentication;
		
		SMTPAuthenticator(String userName, String password) {
			passwordAuthentication = new PasswordAuthentication(userName, password);
		}
		public PasswordAuthentication getPasswordAuthentication() {
			return passwordAuthentication;
		}
	}
}
